using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont endGameFont;

        KeyboardState keyboard;

        bool spaceIsPressed;
        bool spaceWasPressed;

        public bool gameStarted;

        Ball pongBall;
        public Paddle paddle1;
        public Paddle paddle2;

        Texture2D ballImg;
        Texture2D paddleImg;

        Vector2 startingPos;

        public int screenHeight;
        public int screenWidth;

        int player1Score = 0;
        int player2Score = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            spriteBatch = new SpriteBatch(GraphicsDevice);

            screenHeight = graphics.PreferredBackBufferHeight;
            screenWidth = graphics.PreferredBackBufferWidth;

            this.startingPos = new Vector2(screenWidth/2, screenHeight/2);

            spaceIsPressed = false;
            spaceWasPressed = false;
            gameStarted = false;

            pongBall = new Ball(this);
            pongBall.Initialize(startingPos, new Rectangle(0, 0, 25, 25), Color.White,
                             new Vector2(pongBall.source.Width / 2 , pongBall.source.Height / 2 ), 
                             new Vector2(1, 1), 0.0f);

            paddle1 = new Paddle(this);
            paddle1.Initialize(Vector2.Zero, new Rectangle(0, 0, 25, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);

            paddle2 = new Paddle(this, false);
            paddle2.Initialize(new Vector2(screenWidth - 25, 0), new Rectangle(0, 0, 25, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f); 

            base.Initialize();
        }

        public void InitializeWrapper()
        {
            Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            endGameFont = Content.Load<SpriteFont>("EndGameFont");

            ballImg = Content.Load<Texture2D>("spaceCore");
            pongBall.LoadContent(spriteBatch, ref ballImg);

            paddleImg = Content.Load<Texture2D>("paddle");
            paddle1.LoadContent(spriteBatch, ref paddleImg);
            paddle2.LoadContent(spriteBatch, ref paddleImg);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            pongBall.Dispose();
            paddle1.Dispose();
            paddle2.Dispose();
        }

        public void UnloadContentWrapper()
        {
            UnloadContent();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            keyboard = Keyboard.GetState();
            spaceIsPressed = keyboard.IsKeyDown(Keys.Space);

            if (gameStarted == false)
            {
                if (!spaceIsPressed && spaceWasPressed)
                {
                    //Start the game
                    if (pongBall.position.X >= screenWidth- pongBall.source.Width)
                    {
                        player1Score++;
                    }
                    else if (pongBall.position.X <= 0)
                    {
                        player2Score++;
                    }
                    pongBall.Respawn();
                    gameStarted = true;
                    
                }
                spaceWasPressed = spaceIsPressed;
            }
            else
            {
                pongBall.Move();
            }
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            spriteBatch.DrawString(endGameFont, "Player 1 Score: " + player1Score + " Player 2 Score: " + player2Score, new Vector2(200, screenHeight - endGameFont.MeasureString("Magic").Y), Color.White);

            if (gameStarted == false)
            {
                spriteBatch.DrawString(endGameFont, "Press Space to Start", new Vector2(100, 10), Color.White);
                
            }

            spriteBatch.End();
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
